package com.pda.common.pages.coderevert;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.common.basemvp.model.BaseModel;
import com.pda.common.bean.BaseBean;
import com.pda.common.beanin.CodeQueryInBean;
import com.pda.common.tools.StringProcess;
import com.pda.common.utils.PDAUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class CodeRevertModelImpl extends BaseModel implements CodeRevertModel {
    private String tag = "CodeRevertModelImpl";
    private BaseBean baseBean = null;

    public CodeRevertModelImpl(Context context) {
        super(context);
    }

    @Override
    public void codeReset(CodeQueryInBean codeQueryInBean, final CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(codeQueryInBean));
            httpConfig.codeReplace(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            String str="数码重置成功";
//                            if(codeDeatailBean.getData().getCodeType().equals("箱码"))
//                            {
//                                if(codeDeatailBean.getData().getListSub().size()<=0){
//                                    str = "数码为箱码\n"+"此码暂未建立对应关系";
//                                }
//                                else
//                                {
//                                    str = "数码为箱码\n"+"对应盒码:";
//                                    for (int i = 0; i <codeDeatailBean.getData().getListSub().size() ; i++) {
//                                        str= str+codeDeatailBean.getData().getListSub().get(i)+" ,";
//                                    }
//                                    str = str.substring(0, str.length() -1);
//                                }
//                            }
//                            else if(codeDeatailBean.getData().getCodeType().equals("盒码")){
//                                String str1 = codeDeatailBean.getData().getParentCode();
//                                if(str1.equals(""))
//                                {
//                                    str = "数码为盒码\n"+"此码暂未建立对应关系";
//                                }
//                                else
//                                {
//                                    str = "数码为盒码\n"+"对应箱码:"+str1;
//                                }
//                            }
//                            else
//                            {
//                                str = "此码暂未建立对应关系";
//                            }
                            callBack.ontip(str);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBean.getErrmsg() != null && baseBean.getErrmsg().length() > 0) {
                                callBack.ontip(baseBean.getErrmsg());
                            } else {
                                callBack.ontip(PDAUtil.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBean = gson.fromJson(StringProcess.responsetoString(abBean), BaseBean.class);
                            if (baseBean != null && baseBean.getErrcode() == 0) {
                                onComplete();
                            }
                            else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(PDAUtil.connectError);
            e.printStackTrace();
        }
    }
}

